/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.VCProjectEngine;
using EnvDTE;
using EnvDTE80;
using VSLangProj;
using VSLangProj2;
using VSLangProj80;
using System.Collections.ObjectModel;

namespace DaveSexton.DocProject
{
	public class VCDocProject : VSDocProject
	{
		#region Public Properties
		public override string Directory
		{
			get
			{
				return PathFormat.EnsureDirectory(project.ProjectDirectory);
			}
		}

		public override string FilePath
		{
			get
			{
				return project.ProjectFile;
			}
		}

		public override ICollection<ISourceProject> ProjectSources
		{
			get
			{
				List<ISourceProject> sources = new List<ISourceProject>();

				foreach (VCReference reference in (VCReferences) project.VCReferences)
				{
					object source = GetSourceReference(reference);

					if (source != null)
						sources.Add(ProjectFactory.CreateSourceProject(source));
				}

				return sources.AsReadOnly();
			}
		}

		public override SourceCollection AllSources
		{
			get
			{
				SourceCollection sources = new SourceCollection(Provider.AllSupportedExternalSourceFileExtensions);

				foreach (ISourceProject project in ProjectSources)
					sources.Add(new Source(project.Output.OutputFile, project.Output.XmlDocumentationFile));

				sources.AddRange(ExternalSources);

				return sources.AsReadOnly();
			}
		}
		#endregion

		#region Private / Protected
		private readonly VCProject project;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="VCDocProject" /> class.
		/// </summary>
		internal VCDocProject(VCProject project)
			: base((VSProject2) project)
		{
			this.project = project;
		}
		#endregion

		#region Methods
		private static object GetSourceReference(VCReference reference)
		{
			VCProjectReference projectRef = reference as VCProjectReference;

			if (projectRef != null)
			{
				object source = projectRef.ReferencedProject;

				if (source != null && ProjectFactory.IsSourceProject(source))
					return source;
			}

			return null;
		}

		protected override IProjectOutput CreateOutput()
		{
			return new VCProjectOutput(this);
		}

		public override void AddReference(object projectReference)
		{
			project.AddProjectReference(ProjectFactory.GetVSProject(projectReference));
		}
		#endregion
	}
}
